package com.cost.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.cost.service.CostConsumeService;
import com.cost.vo.Entity;
import com.rmb.common.model.response.CommonCode;
import com.rmb.common.model.response.ResponseResult;
import com.rmb.mart.entity.cost.CostConsume;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * (CostConsume)表控制层
 *
 * @author makejava
 * @since 2021-01-20 14:04:17
 */
@RestController
@RequestMapping("/costConsume")
public class CostConsumeController {
    /**
     * 服务对象
     */
    @Resource
    private CostConsumeService costConsumeService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public CostConsume selectOne(Long id) {
        return this.costConsumeService.queryById(id);
    }

    /**
     * 查询所用数据
     *
     * @return 所用数据
     */
    @GetMapping("/selectAll")
    public List<CostConsume> selectAll(){
        return this.costConsumeService.selectAll();
    }

    /**
     * 分页查询
     *
     * @param offset 初始页
     * @param limit 每页
     * @return 每页数据
     */
    @GetMapping("/selectAllByLimit")
    public List<CostConsume> selectAllByLimit(int offset,int limit){
        return this.costConsumeService.queryAllByLimit(offset,limit);
    }
    /**
     * 条件查询
     * @param costConsume 条件
     * @return 条件查询数据
     */
    @PostMapping("/selectAllCondition")
    public List<CostConsume> selectAllCondition(@RequestBody CostConsume costConsume){
        return this.costConsumeService.selectAllCondition(costConsume);
    }

    /**
     * 查询所有金额
     * @return 所用金额
     */
    @GetMapping("/selectConsume")
    public Map<String,Object> selectConsume(){
        return this.costConsumeService.selectConsume();
    }

    /**
     * 查询余额
     * @param id 账户余额id
     * @return 余额数据
     */
    @GetMapping("/selectFindUid")
    public Entity selectFindUid(Long id){
        return this.costConsumeService.selectFindUid(id);
    }

    /**
     * 更改法值
     * @param id
     * @param warningName
     * @param newPhone
     */
    @PostMapping("/add")
    public void add(@RequestParam("id") Long id, @RequestParam("warningName") BigDecimal warningName, @RequestParam("newPhone")String newPhone){
            this.costConsumeService.updateIfo(id,warningName,newPhone);
    }

    @GetMapping("/export")
    public ResponseResult export(String file){
        List<CostConsume> costConsumes = this.costConsumeService.selectAll();

        try {
            ExcelWriterBuilder write = EasyExcel.write(file, CostConsume.class);
            write.sheet().doWrite(costConsumes);
            return new ResponseResult(CommonCode.SUCCESS);
        }catch (Exception e){
            return new ResponseResult(CommonCode.FAIL);
        }
    }



}